<template>
  <div class="login-container">
    <el-form ref="myform" :rules="rules" class="myform" :model="form">
      <h3><img src="../../assets/common/login-logo.png" alt="" /></h3>
      <el-form-item prop="mobile">
        <el-input
          v-model="form.mobile"
          prefix-icon="el-icon-user-solid"
          placeholder="username"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          prefix-icon="el-icon-lock"
          show-password
          placeholder="passwork"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          :loading="loading"
          @click="login"
          class="mybtn"
          type="primary"
          >登录</el-button
        >
      </el-form-item>
      <div class="reg">还没有账号？立即注册</div>
    </el-form>
  </div>
</template>

<script>
import { validateMoblie } from "@/utils/validate";
import { mapActions } from "vuex";
export default {
  data() {
    return {
      loading: false,
      form: {
        mobile: "13800000002",
        password: "123456",
      },
      rules: {
        mobile: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
          { validator: validateMoblie },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 4, max: 10, message: "密码长度4到10位", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    ...mapActions({ userLogin: "user/login" }),
    login() {
        this.loading = true;
        //完成表单校验
        this.$refs.myform.validate(async (res) => {
          if (res) {
            try {
              //校验通过：提交数据到服务器
              //调用vuex中的方法
              await this.userLogin(this.form);
              //接受 _redirect
              const _redirect=this.$route.query._redirect
              //判断是否存在参数 _redirect
              if(_redirect){
                this.$router.push(_redirect)
              }else{
                //跳转到首页
                this.$router.push("/");
              }
              this.$message.success('登录成功')
            } catch (error) {
              console.log("登录时出错" + error.message);
            } finally {
              this.loading = false;
            }
          }
        });
    },
  },
};
</script>


<style lang="scss" >
.login-container {
  height: 100%;
  background-image: url("../../assets/common/login.jpg");
  background-position: center;
  .myform {
    width: 520px;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    h3 {
      margin-bottom: 30px;
    }
    .el-input__inner {
      height: 50px;
      color: #79b0fe;
      background-color: #d4e5ff;
    }
    .mybtn {
      width: 100%;
      height: 60px;
      font-size: 24px;
      background: #3e7efd;
    }
    .reg {
      text-align: center;
      color: #fff;
    }
    .el-form-item__error {
      color: #fff;
    }
  }
}
</style>
